using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace SPGE
{
    public abstract class TransitionScene : AScene
    {
        public IScene Out
        { get; protected set; }

        public IScene In
        { get; protected set; }

        public TimeSpan Duration
        { get; protected set; }

        private TimeSpan Remaining
        { get; set; }

        protected float Percent
        {
            get
            {
                return 1f - (float)(Remaining.TotalSeconds / Duration.TotalSeconds);
            }
        }

        protected static RenderTarget2D InRT, OutRT;

        public override string LoadGroup
        { get { return In.LoadGroup; } }

        /// <summary>
        /// A default implementation of the transition.
        /// </summary>
        /// <param name="outgoing"></param>
        /// <param name="incomming"></param>
        /// <param name="duration"></param>
        public TransitionScene(IScene outgoing, IScene incomming, TimeSpan duration)
            : base(String.Format("Transition{0}{1}", outgoing.Name, incomming.Name))
        {
            Duration = duration;
            Remaining = duration;

            In = incomming;
            Out = outgoing;
        }

        protected override IScene GetNext()
        {
            return In;
        }


        public override void Load(ResourceGroupHandler gm)
        {
            In.Load(gm);
        }

        public override void Initialize()
        {
            inUpdated = false;
            Remaining = Duration;
        }

        bool inUpdated = false;
        public override void Update(GameTime gameTime)
        {
            //We dont call update in the scenes. We just draw them and call update on the incomming 
            // only once, just to set its items.
            if (!inUpdated)
            {
                inUpdated = true;
                In.Update(gameTime);
            }

            Remaining -= gameTime.ElapsedGameTime;

            if (Remaining.TotalMilliseconds <= 0)
            {
                IsDone = true;
                Remaining = TimeSpan.FromSeconds(0);
            }
        }

        protected override IEnumerable<IGameItem> GetGameItems()
        {
            return new List<IGameItem>();
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
        }
    }
}
